# 抖码算法，让算法学习变得简单有趣
# 作者：老汤


class TreeNode:
    def __init__(self, val, left, right):
        self.val = val;
        self.left = left
        self.right = right


res = 0


# DFS 前序遍历 递归
def maxDepth3(root: TreeNode) -> int:
    if not root:
        return 0
    preorder(root, 1)
    return res


def preorder(node: TreeNode, curr_level: int) -> None:
    if not node:
        return
    global res
    res = max(res, curr_level)

    preorder(node.left, curr_level + 1)
    preorder(node.right, curr_level + 1)


# DFS 后序遍历 递归
def maxDepth(self, root: TreeNode) -> int:
    if not root:
        return 0

    leftMaxDepth = maxDepth(root.left)
    rightMaxDepth = maxDepth(root.right)

    return max(leftMaxDepth, rightMaxDepth) + 1